<template>
  <div>
    <el-dialog title="新增部门" :visible="showDialog" @close="close">
      <el-form label-width="120px" :model="formDate" ref="add" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input
            v-model="formDate.name"
            style="width: 80%"
            placeholder="2-10个字符"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input
            v-model="formDate.code"
            style="width: 80%"
            placeholder="2-10个字符"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item label="部门负责人" prop="managerId">
          <el-select
            placeholder="请选择负责人"
            v-model="formDate.managerId"
            size="mini"
            style="width: 80%"
          >
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="formDate.introduce"
            type="textarea"
            :rows="4"
            style="width: 80%"
            placeholder="1-100个字符"
            size="mini"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" @click="btnOk">确认</el-button>
              <el-button @click="close">取消</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartment, getManagerList, addDepartment } from "@/api/department";
export default {
  data() {
    return {
      managerList: [], //存储负责人的数据
      formDate: {
        code: "", //部门编码
        introduce: "", //部门介绍
        managerId: "", //部门负责人名字
        name: "", //部门名称
        pid: "", //部门父级部门id
      },
      rules: {
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "部门编码的长度为2-10个字符",
            trigger: "blur",
          },
          {
            trigger: "blur",
            // 自定义校验模式
            validator: async (rule, value, callback) => {
              // value就是输入的编码
              const result = await getDepartment();
              // result数组中是否存在 value值
              if (result.some((item) => item.code === value)) {
                callback(new Error("部门中已经有该编码了"));
              } else {
                callback();
              }
            },
          },
        ], // 部门编码
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {
            min: 1,
            max: 100,
            message: "部门介绍的长度为1-100个字符",
            trigger: "blur",
          },
        ], // 部门介绍
        managerId: [
          { required: true, message: "部门负责人不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "部门名称的长度为2-10个字符",
            trigger: "blur",
          },
          {
            trigger: "blur",
            // 自定义校验模式
            validator: async (rule, value, callback) => {
              // value就是输入的编码
              const result = await getDepartment();
              // result数组中是否存在 value值
              if (result.some((item) => item.name === value)) {
                callback(new Error("部门中已经有该名称了"));
              } else {
                callback();
              }
            },
          },
        ], // 部门名称
      },
    };
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
    currentNodeId: {
      type: Number,
      default: null,
    },
  },
  created() {
    this.getManagerList();
  },
  methods: {
    close() {
      // 这里是子组件需要修改父组件的属性  使用自定义事件
      this.$emit("update:showDialog", false);
      this.$refs.add.resetFields(); // 重置表单
    },
    // 获取部门的列表数据
    async getManagerList() {
      const res = await getManagerList();
      this.managerList = res;
    },
    // 点击确实调用
    btnOk() {
      this.$refs.add.validate(async (isOk) => {
        if (isOk) {
          await addDepartment({ ...this.formDate, pid: this.currentNodeId });
          //通知父组件进行更行
          this.$emit("updateDepartment");
          this.$message.success("新增部门成功");
          this.close();
        }
      });
    },
  },
};
</script>

<style></style>
